var pictureIds = [];
var t;
$(function () {
  getPicturesBySetId();
    selectLoad();
});

function getPicturesBySetId() {
    var pictureSetId = $("#pictureSetId").val();
    var stationId = $('#stationId').val();
    // t = setInterval(function () {
        $.post("/collection/picture/getByPictureSetId",
            {
                pictureSetId:pictureSetId,
                stationId:stationId
            },function(data){
                // if(data == null || data == ''){
                //     return;
                // }else {
                //     clearInterval(t);
                // }
                var pics = '';
                $.each(data,function (index) {
                    pictureIds.push(data[index].pictureId);
                    pics += "<div name='pics' data-id=\""+data[index].pictureId + "\" class=\"img-row\">\n" +
                        "<img src=" + data[index].pictureUrl+">\n" +
                        "<b onclick='del("+data[index].pictureId+")'>x</b></div>";
                });
                pics+='<div id ="addIcon" class="img-row"><i onclick="openLibrary()" class="icon iconfont icon-font-add"></i></div>'
                $("#pictureContatainer").append(pics);
            })
    // },1500);
}


function selectLoad() {
    var html = "";
    $.get("/collection/pushStation/getPushStation",
        function(data) {
            //加载数据
            for (var i = 0; i < data.length; i++) {
                if(data[i].isPush == 1){
                    html += '<div class="checkdiv"><input id="check_'+i+'" name="station"  type="checkbox" checked="checked" value="'+data[i].pushStationId+'"/>' +
                        '<label for="check_'+i+'">'+data[i].pushStationName+'</label></div>'
                }else {
                    html += '<div class="checkdiv"><input id="check_'+i+'" name="station"  type="checkbox"  value="'+data[i].pushStationId+'"/>' +
                        '<label for="check_'+i+'">'+data[i].pushStationName+'</label></div>'
                }

            }
            $("#pushStation").append(html);
            $(".chosen-select").chosen({
                maxHeight : 200
            });
            //点击事件
            $('.chosen-select').on('change', function(e, params) {
                console.log(params.selected);
                var opt = {
                    query : {
                        type : params.selected,
                    }
                }
            });
        }
    );
}


function del(pictureId) {
    $("[data-id="+pictureId+"]").remove();
    // $("#"+pictureId).remove();
    pictureIds.splice($.inArray(pictureId,pictureIds),1);
    // $.ajax({
    //     url : "/collection/picture/deleteByPictureId",
    //     type : "post",
    //     data : {
    //         'pictureId' : pictureId
    //     },
    //     success : function(r) {
    //         if (r.code == 0) {
    //             layer.msg(r.msg);
    //             location.reload();
    //         } else {
    //             layer.msg(r.msg);
    //         }
    //     }
    // });
}


function upload(){
    var chk_value =[];
    var pictureSetId = $("#pictureSetId").val();
    var title = $('#title').val();
    chk_value = getCheckBoxVal();
    if(chk_value.length==0 ){
        alert('你还没有选择上传站点')
        return;
    }
    $.post(
        "/collection/picture/uploadPicture",
       {
           pictureSetId: pictureSetId,
           title: title,
           station: chk_value,
           pictureIds: pictureIds
       }
   );
}

function returnList() {
    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
    parent.layer.close(index);
}

function getCheckBoxVal(){ //jquery获取所有选中的复选框的值 
    var chk_value =[];        
    $("#signupForm").find('input[name="station"]:checked').each(function(){ //遍历，将所有选中的值放到数组中       
    chk_value.push($(this).val());    
     });
    return chk_value;
}

function openLibrary() {
    var keyTitle = $("#keyTitle").val();
    var addPage = layer.open({
        type :2,
        title : '伪原创库',
        maxmin : true,
        shadeClose : false, //点击遮罩关闭层
        area : [ '800px', '520px' ],
        content : '/collection/picture/openLibrary/' + keyTitle// iframe的url
    });
    layer.full(addPage);
}

function addAtlas(ids) {
    var pictureSetId = $("#pictureSetId").val();
    var stationId = $('#stationId').val();
    var keyTitle = $("#keyTitle").val();
    $.post(
        "/collection/picture/addAtlas",
        {
            pictureSetId: pictureSetId,
            ids: ids,
            stationId: stationId,
            keyTitle: keyTitle
        },
        function (data) {
            $("#addIcon").remove();
            var pics = '';
            $.each(data,function (index) {
                pictureIds.push(data[index].pictureId);
                pics += "<div name='pics' data-id=\""+data[index].pictureId + "\" class=\"img-row\">\n" +
                    "<img src=" + data[index].pictureUrl+">\n" +
                    "<b onclick='del("+data[index].pictureId+")'>x</b></div>";
            });
            pics+='<div id = "addIcon" class="img-row"><i onclick="openLibrary()" class="icon iconfont icon-font-add"></i></div>';
            $("#pictureContatainer").append(pics);
        }
    )
}


function deleteFromLibrary(deleteIds) {
    var keyTitle = $("#keyTitle").val();
    $.get(
        "/collection/picture/deleteFromLibrary",
        {
            keyTitle: keyTitle,
            ids: deleteIds
        }
    )
}